home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / pcb-1.000 / pcb-1 / pcb-1.3 / sizedialog.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  10KB  |  315 lines

  1. /*
  2.  *                            COPYRIGHT
  3.  *
  4.  *  PCB, interactive printed circuit board design
  5.  *  Copyright (C) 1994,1995 Thomas Nau
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *  Contact addresses for paper mail and Email:
  22.  *  Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
  23.  *  Thomas.Nau@rz.uni-ulm.de
  24.  *
  25.  */
  26.  
  27. static    char    *rcsid = "$Header: /sda4/users/nau/src/pcb/RCS/sizedialog.c,v 2.1 1994/10/29 17:24:57 nau Exp $";
  28.  
  29. /* size dialog routines
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34.  
  35. #include "global.h"
  36.  
  37. #include "crosshair.h"
  38. #include "data.h"
  39. #include "dialog.h"
  40. #include "error.h"
  41. #include "misc.h"
  42. #include "sizedialog.h"
  43. #include "set.h"
  44.  
  45. #include <X11/Shell.h>
  46. #include <X11/Xaw/AsciiText.h>
  47. #include <X11/Xaw/Command.h>
  48. #include <X11/Xaw/Form.h>
  49. #include <X11/Xaw/Label.h>
  50. #include <X11/Xaw/Scrollbar.h>
  51. #include <X11/Xaw/Toggle.h>
  52.  
  53. /* ---------------------------------------------------------------------------
  54.  * define resource for X11R4 (names have changed from R4 to R5)
  55.  * and scrollbar sizes
  56.  */
  57. #ifndef    XtNtopOfThumb
  58. #define    XtNtopOfThumb    XtNtop
  59. #endif
  60.  
  61. #define    MIN_SCALE            0.5
  62. #define    MAX_SCALE            3.0
  63. #define    THUMB_LENGTH        10
  64. #define    SCROLLBAR_LENGTH    250
  65.  
  66. #define    LINE_SLIDER            0
  67. #define    VIADRILL_SLIDER        1
  68. #define    VIASIZE_SLIDER        2
  69. #define    WIDTH_SLIDER        3
  70. #define    HEIGHT_SLIDER        4
  71.  
  72. /* ---------------------------------------------------------------------------
  73.  * some local types
  74.  */
  75. typedef struct
  76. {
  77.     String        Label;            /* label to display */
  78.     Dimension    Min,            /* limits of scale */
  79.                 Max,
  80.                 Step,            /* one step */
  81.                 Value;
  82.     Widget        Scrollbar,        /* some widgets */
  83.                 Size;            /* size field (label widget) */
  84. } SliderType, *SliderTypePtr;
  85.  
  86. /* ---------------------------------------------------------------------------
  87.  * some local identifiers
  88.  */
  89. static    int            ReturnCode;            /* returncode of buttons */
  90. static    SliderType    Sliders[] = {
  91.     { "linewidth", MIN_LINESIZE, MAX_LINESIZE, 5, 0, NULL, NULL },
  92.     { "via hole", MIN_PINORVIAHOLE, 0, 5, 0, NULL, NULL },
  93.     { "via size", 0, MAX_PINORVIASIZE, 5, 0, NULL, NULL },
  94.     { "PCB width", MIN_SIZE, MAX_COORD, 100, 0, NULL, NULL },
  95.     { "PCB height", MIN_SIZE, MAX_COORD, 100, 0, NULL, NULL }};
  96.  
  97. /* ---------------------------------------------------------------------------
  98.  * some local prototypes
  99.  */
  100. static    void        UpdateSlider(SliderTypePtr);
  101. static    Dimension    UpdateThumb(SliderTypePtr, Dimension);
  102. static    void        UpdateScrollbar(SliderTypePtr, Dimension);
  103. static    void        CB_CancelOrOK(Widget, XtPointer, XtPointer);
  104. static    void        CB_ScrollProc(Widget, XtPointer, XtPointer);
  105. static    void        CB_JumpProc(Widget, XtPointer, XtPointer);
  106.  
  107. /* ---------------------------------------------------------------------------
  108.  * updates a slider
  109.  */
  110. static void UpdateSlider(SliderTypePtr Slider)
  111. {
  112.     char    s[10];
  113.  
  114.     sprintf(s, "%i", Slider->Value);
  115.     XtVaSetValues(Slider->Size, XtNlabel, s, NULL);
  116. }
  117.  
  118. /* ---------------------------------------------------------------------------
  119.  * clips the value to fit into min/max range and updates the widget
  120.  * returns the clipped value
  121.  */
  122. static Dimension UpdateThumb(SliderTypePtr Slider, Dimension NewValue)
  123. {
  124.     float    top;
  125.  
  126.         /* set new value, reduce to valid 'step' values */
  127.     NewValue = (NewValue /Slider->Step) *Slider->Step;
  128.     NewValue = MIN(NewValue, Slider->Max);
  129.     NewValue = MAX(NewValue, Slider->Min);
  130.     Slider->Value = NewValue;
  131.  
  132.         /* prevent from floating point errors */
  133.     if (Slider->Max == Slider->Min)
  134.         top = 1.0;
  135.     else
  136.         top = (float) (Slider->Value -Slider->Min) / (float) (Slider->Max -Slider->Min);
  137.     top = MIN(top, (1.0 -((float) THUMB_LENGTH /(float) SCROLLBAR_LENGTH)));
  138.  
  139.         /* change position only */
  140.     XawScrollbarSetThumb(Slider->Scrollbar, top, -1.0);
  141.     return(NewValue);
  142. }
  143.  
  144. /* ---------------------------------------------------------------------------
  145.  * updates the position of the scrollbar thumb
  146.  * the thumb position is passed; related scrollbars are updated too
  147.  */
  148. static void UpdateScrollbar(SliderTypePtr Slider, Dimension NewValue)
  149. {
  150.     NewValue = UpdateThumb(Slider, NewValue);
  151.     UpdateSlider(Slider);
  152.  
  153.         /* changing of via thickness might cause the drilling hole slider
  154.          * to change it's limits and vice versa
  155.          */
  156.     if (Slider == &Sliders[VIADRILL_SLIDER])
  157.     {
  158.             /* set lower limit of via size slider and recalculate
  159.              * its current value
  160.              */
  161.         Sliders[VIASIZE_SLIDER].Min = NewValue +MIN_PINORVIACOPPER;
  162.         UpdateThumb(&Sliders[VIASIZE_SLIDER], Sliders[VIASIZE_SLIDER].Value);
  163.         UpdateSlider(&Sliders[VIASIZE_SLIDER]);
  164.     }
  165.     if (Slider == &Sliders[VIASIZE_SLIDER])
  166.     {
  167.             /* set lower limit of via drilling hole slider and recalculate
  168.              * its current value
  169.              */
  170.         Sliders[VIADRILL_SLIDER].Max = NewValue -MIN_PINORVIACOPPER;
  171.         UpdateThumb(&Sliders[VIADRILL_SLIDER], Sliders[VIADRILL_SLIDER].Value);
  172.         UpdateSlider(&Sliders[VIADRILL_SLIDER]);
  173.     }
  174. }
  175.  
  176. /* ---------------------------------------------------------------------------
  177.  * callback function for OK and cancel button
  178.  */
  179. static void CB_CancelOrOK(Widget W, XtPointer ClientData, XtPointer CallData)
  180. {
  181.     ReturnCode = (int) ClientData;
  182. }
  183.  
  184. /* ---------------------------------------------------------------------------
  185.  * callback function for scrolling
  186.  * see Athena Widget manual for details
  187.  * the scrollwidth is replaced by a constant
  188.  */
  189. static void CB_ScrollProc(Widget W, XtPointer ClientData, XtPointer CallData)
  190. {
  191.     int                delta = (int) CallData;
  192.     SliderTypePtr    slider = (SliderTypePtr) ClientData;
  193.  
  194.     UpdateScrollbar(slider,
  195.         slider->Value + (delta >= 0 ? slider->Step : -slider->Step));
  196. }
  197.  
  198. /* ---------------------------------------------------------------------------
  199.  * callback function for scrolling
  200.  * see Athena Widget manual for details
  201.  */
  202. static void CB_JumpProc(Widget W, XtPointer ClientData, XtPointer CallData)
  203. {
  204.     float            top = *(float *) CallData;
  205.     SliderTypePtr    slider = (SliderTypePtr) ClientData;
  206.  
  207.     UpdateScrollbar(slider,
  208.         (Dimension) (top *(slider->Max-slider->Min) +(float) slider->Min +0.5));
  209. }
  210.  
  211. /* ---------------------------------------------------------------------------
  212.  * size dialog
  213.  */
  214. void SizeDialog(void)
  215. {
  216.             Widget                popup,
  217.                                 masterform,
  218.                                 label,
  219.                                 last;
  220.             int                    i;
  221.     static    DialogButtonType    buttons[] = {
  222.         { "defaultButton", "   OK   ", CB_CancelOrOK,
  223.             (XtPointer) OK_BUTTON, NULL },
  224.         { "cancelButton", "No/Cancel", CB_CancelOrOK,
  225.             (XtPointer) CANCEL_BUTTON, NULL }};
  226.  
  227.         /* copy current values to struct */
  228.     Sliders[LINE_SLIDER].Value = Settings.LineThickness;
  229.     Sliders[VIADRILL_SLIDER].Value = Settings.ViaDrillingHole;
  230.     Sliders[VIADRILL_SLIDER].Max = Settings.ViaThickness -MIN_PINORVIACOPPER;
  231.     Sliders[VIASIZE_SLIDER].Value = Settings.ViaThickness;
  232.     Sliders[VIASIZE_SLIDER].Min = Settings.ViaDrillingHole +MIN_PINORVIACOPPER;
  233.     Sliders[WIDTH_SLIDER].Value = PCB->MaxWidth;
  234.     Sliders[HEIGHT_SLIDER].Value = PCB->MaxHeight;
  235.  
  236.         /* create a popup shell */
  237.     popup = XtVaCreatePopupShell("popup", transientShellWidgetClass,
  238.         Output.Toplevel,
  239.         XtNallowShellResize, True,
  240.         XtNmappedWhenManaged, False,
  241.         NULL);
  242.  
  243.         /* the form that holds everything */
  244.     masterform = XtVaCreateManagedWidget("sizeMasterForm", formWidgetClass,
  245.         popup,
  246.         XtNresizable, True,
  247.         NULL);
  248.  
  249.         /* create the sliders */
  250.     last = NULL;
  251.     for (i = 0; i < ENTRIES(Sliders); i++)
  252.     {
  253.         label = XtVaCreateManagedWidget("comment", labelWidgetClass,
  254.             masterform,
  255.             XtNfromVert, last,
  256.             XtNlabel, Sliders[i].Label,
  257.             LAYOUT_TOP,
  258.             NULL);
  259.         Sliders[i].Size = XtVaCreateManagedWidget("size", labelWidgetClass,
  260.             masterform,
  261.             XtNfromVert, last,
  262.             XtNfromHoriz, label,
  263.             XtNresizable, True,
  264.             LAYOUT_TOP,
  265.             NULL);
  266.         last = XtVaCreateManagedWidget("scrollbar", scrollbarWidgetClass,
  267.             masterform,
  268.             LAYOUT_TOP,
  269.             XtNfromVert, label,
  270.             XtNlength, SCROLLBAR_LENGTH,
  271.             XtNminimumThumb, THUMB_LENGTH,
  272.             XtNorientation, XtorientHorizontal,
  273.             NULL);
  274.         Sliders[i].Scrollbar = last;
  275.  
  276.             /* add callbacks for scrolling */
  277.         XtAddCallback(Sliders[i].Scrollbar,
  278.             XtNjumpProc, CB_JumpProc, &Sliders[i]);
  279.         XtAddCallback(Sliders[i].Scrollbar,
  280.             XtNscrollProc, CB_ScrollProc, &Sliders[i]);
  281.     }
  282.  
  283.         /* now that all sliders are initialized --> do an update */
  284.     for (i = 0; i < ENTRIES(Sliders); i++)
  285.         UpdateScrollbar(&Sliders[i], Sliders[i].Value);
  286.  
  287.         /* add the buttons and install accelerators for them;
  288.          * the first one is always default
  289.          */
  290.     AddButtons(masterform, last, buttons, ENTRIES(buttons));
  291.     XtInstallAccelerators(masterform, buttons[0].W);
  292.     XtInstallAccelerators(masterform, buttons[1].W);
  293.  
  294.         /* now display dialog window */
  295.     StartDialog(popup);
  296.     DialogEventLoop(&ReturnCode);
  297.  
  298.         /* get settings */
  299.     if (ReturnCode == OK_BUTTON)
  300.     {
  301.         SetLineSize(Sliders[LINE_SLIDER].Value);
  302.         SetViaDrillingHole(Sliders[VIADRILL_SLIDER].Value);
  303.         SetViaSize(Sliders[VIASIZE_SLIDER].Value);
  304.  
  305.             /* set new maximum size and update scrollbars */
  306.         if (PCB->MaxWidth != Sliders[WIDTH_SLIDER].Value ||
  307.             PCB->MaxHeight != Sliders[HEIGHT_SLIDER].Value)
  308.         {
  309.             ChangePCBSize(Sliders[WIDTH_SLIDER].Value,
  310.                 Sliders[HEIGHT_SLIDER].Value);
  311.         }
  312.     }
  313.     EndDialog(popup);
  314. }
  315.